package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.TProductCategoryMapper;
import com.ruoyi.business.domain.TProductCategory;
import com.ruoyi.business.service.ITProductCategoryService;

/**
 * 类目信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-12
 */
@Service
public class TProductCategoryServiceImpl implements ITProductCategoryService 
{
    @Autowired
    private TProductCategoryMapper tProductCategoryMapper;

    /**
     * 查询类目信息
     * 
     * @param categoryId 类目信息主键
     * @return 类目信息
     */
    @Override
    public TProductCategory selectTProductCategoryByCategoryId(Long categoryId)
    {
        return tProductCategoryMapper.selectTProductCategoryByCategoryId(categoryId);
    }

    /**
     * 查询类目信息列表
     * 
     * @param tProductCategory 类目信息
     * @return 类目信息
     */
    @Override
    public List<TProductCategory> selectTProductCategoryList(TProductCategory tProductCategory)
    {
        return tProductCategoryMapper.selectTProductCategoryList(tProductCategory);
    }

    /**
     * 新增类目信息
     * 
     * @param tProductCategory 类目信息
     * @return 结果
     */
    @Override
    public int insertTProductCategory(TProductCategory tProductCategory)
    {
        tProductCategory.setCreateTime(DateUtils.getNowDate());
        return tProductCategoryMapper.insertTProductCategory(tProductCategory);
    }

    /**
     * 修改类目信息
     * 
     * @param tProductCategory 类目信息
     * @return 结果
     */
    @Override
    public int updateTProductCategory(TProductCategory tProductCategory)
    {
        tProductCategory.setUpdateTime(DateUtils.getNowDate());
        return tProductCategoryMapper.updateTProductCategory(tProductCategory);
    }

    /**
     * 批量删除类目信息
     * 
     * @param categoryIds 需要删除的类目信息主键
     * @return 结果
     */
    @Override
    public int deleteTProductCategoryByCategoryIds(Long categoryIds)
    {
        return tProductCategoryMapper.deleteTProductCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除类目信息信息
     * 
     * @param categoryId 类目信息主键
     * @return 结果
     */
    @Override
    public int deleteTProductCategoryByCategoryId(Long categoryId)
    {
        return tProductCategoryMapper.deleteTProductCategoryByCategoryId(categoryId);
    }

    /**
     * 构建前端结构树
     *
     * @param categoryId 类目信息主键
     * @return 结果
     */
    @Override
    public List<TProductCategory> buildCategoryTree(List<TProductCategory> tProductCategories) {
        List<TProductCategory> returnList = new ArrayList<TProductCategory>();
        List<Long> tempList = tProductCategories.stream().map(TProductCategory::getCategoryId).collect(Collectors.toList());
        for (TProductCategory category : tProductCategories)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(category.getParentId()))
            {
                recursionFn(tProductCategories, category);
                returnList.add(category);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = tProductCategories;
        }
        return returnList;
    }

    /**
     * 唯一值
     * @param tProductCategory
     * @return
     */
    @Override
    public boolean checkDeptNameUnique(TProductCategory tProductCategory) {

        System.out.println("tProductCategory==>"+tProductCategory);

        Long deptId = StringUtils.isNull(tProductCategory.getCategoryId()) ? -1L : tProductCategory.getCategoryId();
        TProductCategory info = tProductCategoryMapper.checkDeptNameUnique(tProductCategory.getCategoryName(), tProductCategory.getParentId());
        if (StringUtils.isNotNull(info) && info.getCategoryId().longValue() != deptId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }


    @Override
    public void checkCategoryDataScope(Long deptId) {

    }

    @Override
    public int selectNormalChildrenCategoryById(Long categoryId) {
        return tProductCategoryMapper.selectNormalChildrenDeptById(categoryId);
    }

    @Override
    public boolean checkCategoryNameUnique(TProductCategory tProductCategory) {
        Long deptId = StringUtils.isNull(tProductCategory.getCategoryId()) ? -1L : tProductCategory.getCategoryId();
        TProductCategory info = tProductCategoryMapper.checkDeptNameUnique(tProductCategory.getCategoryName(), tProductCategory.getParentId());
        if (StringUtils.isNotNull(info) && info.getCategoryId().longValue() != deptId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    @Override
    public List<TProductCategory> productCategoryTree(TProductCategory tProductCategory) {
        // 从数据库获取所有产品分类
        List<TProductCategory> tProductCategories = tProductCategoryMapper.selectTProductCategoryList(tProductCategory);
        // 构建树形结构
        List<TProductCategory> tree = buildCategoryTreePlus(tProductCategories, 0L); // 假设0L是根节点的parentId
        return tree;
    }

    private List<TProductCategory> buildCategoryTreePlus(List<TProductCategory> categories, long parentId) {
        // 存储树形结构
        List<TProductCategory> tree = new ArrayList<>();

        // 遍历所有分类
        for (TProductCategory category : categories) {
            // 如果当前分类的parentId与给定的parentId相等，则是该父分类的子分类
            if (category.getParentId().equals(parentId)) {
                // 获取该父分类下的子分类
                List<TProductCategory> children = buildCategoryTreePlus(categories, category.getCategoryId());
                category.setChildren(children); // 设置子分类
                tree.add(category); // 添加到树形结构中
            }
        }
        return tree;
    }


    /**
     * 递归列表
     */
    private void recursionFn(List<TProductCategory> list, TProductCategory t)
    {
        // 得到子节点列表
        List<TProductCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (TProductCategory tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<TProductCategory> getChildList(List<TProductCategory> list, TProductCategory t)
    {
        List<TProductCategory> tlist = new ArrayList<TProductCategory>();
        Iterator<TProductCategory> it = list.iterator();
        while (it.hasNext())
        {
            TProductCategory n = it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getCategoryId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<TProductCategory> list, TProductCategory t)
    {
        return getChildList(list, t).size() > 0;
    }

}
